package cn.edu.sdjzu.xg.eagle.dao.mapper;

import cn.edu.sdjzu.xg.eagle.dao.CommonDao;
import cn.edu.sdjzu.xg.eagle.dao.TeacherDao;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.factory.DaoFactory;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;
import cn.edu.sdjzu.xg.eagle.util.Pagination;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * @author tg.si@188.com
 * @project xg23
 * @date 2025/3/26
 */
public class TeacherDaoImpl implements TeacherDao {
    @Override
    public Teacher find(Connection connection, Long id) throws SQLException, BaseException, InvocationTargetException {
        //创建查询教师的主语句
        String sql_select = "SELECT * from bysj22.teacher WHERE id = ? ";
        //获得查询教师的语句对象
        PreparedStatement pstmt_select = connection.prepareStatement(sql_select);
        pstmt_select.setLong(1, id);
        //获得教师对象的结果集
        ResultSet resultSet_select = pstmt_select.executeQuery();
        Teacher desiredTeacher = null;
        //遍历结果集，根据每个元素创建对象，并添加到集合类对象desiredTeachers中
        if(resultSet_select.next()) {
            String name = resultSet_select.getString("name");
            String no = resultSet_select.getString("no");
//            int departmentId = resultSet_select.getInt("department_id");
//            Department department = ServiceFactory.
//                    getService(DepartmentService.class,"DepartmentServiceImpl").
//                    find(departmentId);
            desiredTeacher = new Teacher(id, name, no);
        }
        //关闭ResultSet, statement资源
        JdbcHelper.close(resultSet_select, pstmt_select);
        return desiredTeacher;
    }

    //写入Teacher表及关联的User信息
    @Override
    public boolean add(Connection connection, Teacher teacher)
            throws SQLException, BaseException, InvocationTargetException {
        //创建添加教师的主语句
        String sql_select = "INSERT INTO bysj22.teacher (id, no, name, user_id) " +
                "VALUES (?, ?, ?, ?)";
        PreparedStatement add_preparedStatement =
                connection.prepareStatement(sql_select);
        //给参数赋值
        add_preparedStatement.setLong(1, teacher.getId());
        add_preparedStatement.setString(2, teacher.getNo());
        add_preparedStatement.setString(3, teacher.getName());
        User user = teacher.getUser();
        if(user != null) {
            //用teacher对象的user字段的id为teacher表外键user_id赋值
            add_preparedStatement.setInt(4, teacher.getUser().getId());
        }
        //执行SQL
        int affectedRowNum = add_preparedStatement.executeUpdate();
        return affectedRowNum > 0;
    }

    @Override
    public boolean update(Connection connection, Teacher teacher) throws SQLException, BaseException, InvocationTargetException {
        return false;
    }

    @Override
    public boolean delete(Connection connection, Integer id) throws SQLException, BaseException, InvocationTargetException {
        return false;
    }

    @Override
    public Collection<Teacher> findAll(
            Connection connection, List<Condition> conditionList,
            Pagination pagination) throws SQLException, BaseException, InvocationTargetException {
        //创建Teacher类型集合类对象，HashSet不保证结果中元素的顺序
        Collection<Teacher> desiredTeachers = new HashSet<Teacher>();
        //创建查询教师的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * from bysj22.teacher ");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if (conditionList != null) {
            String whereClause = Condition.toWhereClause(conditionList);
            sql_select.append(whereClause);
        }
        //获取Teacher中的元组总数
        int totalNum = DaoFactory.getDao(CommonDao.class,"CommonDaoImpl").count(connection, "Teacher", conditionList);

        String paginationClause = pagination.toLimitClause(totalNum);
        sql_select.append(paginationClause);

        //获得查询教师的语句对象
        PreparedStatement pstmt_select = connection.prepareStatement(sql_select.toString());
        //获得教师对象的结果集
        ResultSet resultSet_select = pstmt_select.executeQuery();
        //遍历结果集，根据每个元素创建对象，并添加到集合类对象desiredTeachers中
        while(resultSet_select.next()) {
            Long id = resultSet_select.getLong("id");
            String name = resultSet_select.getString("name");
            String no = resultSet_select.getString("no");
            int departmentId = resultSet_select.getInt("department_id");
//            Department department = ServiceFactory.
//                    getService(DepartmentService.class,"DepartmentServiceImpl").
//                    find(departmentId);
            Teacher desiredTeacher = new Teacher(id, name, no);
            desiredTeachers.add(desiredTeacher);
        }
        //关闭ResultSet, statement资源
        JdbcHelper.close(resultSet_select, pstmt_select);
        return desiredTeachers;
    }


}
